package com.haust.itemcategory.com.sxt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.haust.commons.pojo.HaustResult;
import com.haust.itemcategory.com.sxt.service.ItemCategoryService;
import com.haust.itemcategory.com.sxt.dto.ItemCatDto;
import com.haust.mapper.TbItemCatMapper;
import com.haust.pojo.TbItemCat;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ItemCategoryServiceImpl implements ItemCategoryService {
    @Autowired
    private TbItemCatMapper tbItemCatMapper;

    /**
     * 根据  根据 parentId查询对应的商品分类
     *
     * @param parentId 商品的父节点的Id
     * @return
     */
    @Override
    public HaustResult selectItemCategoryByParentId(Long parentId) {
        QueryWrapper<TbItemCat> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id", parentId)
                .eq("status", 1)
                .orderByAsc("sort_order");
        List<TbItemCat> tbItemCats = tbItemCatMapper.selectList(queryWrapper);


        ArrayList<ItemCatDto> dtos = new ArrayList<>(tbItemCats.size());
        for (TbItemCat tbItemCat : tbItemCats) {
            ItemCatDto dto = new ItemCatDto();

            BeanUtils.copyProperties(tbItemCat, dto);
            dtos.add(dto);
        }

        return HaustResult.ok(dtos);
    }
}
